package com.yuanrui.yrclub.oss.service.impl;

import com.yuanrui.yrclub.oss.entity.FileInfo;
import com.yuanrui.yrclub.oss.service.StorageAdapter;
import com.yuanrui.yrclub.oss.utils.AliUtil;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

/**
 * 阿里云存储服务实现
 */
public class AliStorageAdapterImpl implements StorageAdapter {
    @Resource
    private AliUtil aliUtil;

    @Override
    public void createBucket(String bucket) {
        aliUtil.createBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile uploadFile, String bucket, String objectName) {
        aliUtil.createBucket(bucket);
        if (objectName != null) {
            aliUtil.uploadFile(uploadFile.getInputStream(), bucket, objectName + "/" + uploadFile.getOriginalFilename());
        } else {
            aliUtil.uploadFile(uploadFile.getInputStream(), bucket, uploadFile.getOriginalFilename());
        }
    }

    @Override
    @SneakyThrows
    public List<String> getAllBucket() {
        List<String> allBucket = aliUtil.getAllBucket();
        return allBucket == null ? new LinkedList<>() : allBucket;
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucket) {
        return aliUtil.getAllFile(bucket);
    }

    @Override
    @SneakyThrows
    public InputStream downLoad(String bucket, String objectName) {
        return aliUtil.downLoad(bucket, objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucket) {
        aliUtil.deleteBucket(bucket);
    }

    @Override
    @SneakyThrows
    public void deleteObject(String bucket, String objectName) {
        aliUtil.deleteObject(bucket, objectName);
    }

    @Override
    public String getUrl(String bucket, String objectName) {
        return aliUtil.getUrl(bucket, objectName);
    }


}
